package com.beiye.drivertransport.thematic.learning.examiantion;

import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.AppCompatTextView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.beiye.drivertransport.R;
import com.beiye.drivertransport.activity.TwoBaseAty;
import com.beiye.drivertransport.bean.CertificateCompletionBean;
import com.beiye.drivertransport.bean.CheckBean;
import com.beiye.drivertransport.bean.LoginUserBean;
import com.beiye.drivertransport.http.Login;
import com.beiye.drivertransport.utils.QRCodeUtil;
import com.beiye.drivertransport.utils.TiShiDialog;
import com.beiye.drivertransport.utils.UserManger;
import com.githang.statusbar.StatusBarCompat;

import java.text.SimpleDateFormat;
import java.util.Date;


import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

//继续教育结业界面
public class ContinuingEducationActivity extends TwoBaseAty {

    ImageView img_education_back;

    TextView tv_education1;//编号

    AppCompatTextView tv_education2;

    AppCompatTextView tv_education3;

    AppCompatTextView tv_education4;
//    @Bind(R.id.tv_education5)
//    TextView tv_education5;

    ImageView img_code;//二维码

    TextView tv_education11;//结束日期
    private String begindateStr;
    private String enddateStr;
    private String enddateStr1;


    @Override
    public int getLayoutId() {
        return R.layout.activity_continuing_education;
    }

    public void onClick(View view) {
        if (view.getId() == R.id.img_education_back) {
            finish();
        }

    }
    @Override
    public void initData() {
        img_education_back = (ImageView) findViewById(R.id.img_education_back);
        tv_education1 = (TextView)findViewById(R.id.tv_education1);
        tv_education2 = (AppCompatTextView) findViewById(R.id.tv_education2);
        tv_education3 = (AppCompatTextView) findViewById(R.id.tv_education3);
        tv_education4 = (AppCompatTextView) findViewById(R.id.tv_education4);
        img_code = (ImageView)findViewById(R.id.img_code);
        tv_education11 = (TextView)findViewById(R.id.tv_education11);
        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#ffffff"));


    }

    @Override
    public void requestData() {
        Bundle bundle = getIntent().getExtras();
        String orgId = bundle.getString("orgId");
        int stId = bundle.getInt("stId");
        int uschSn = bundle.getInt("uschSn");
        LoginUserBean userInfo = UserManger.getUserInfo();
        LoginUserBean.DataBean databean = userInfo.getData();
        String userId = databean.getUserId();
        new Login().getCertificateofCompletionQuery(userId, orgId, stId, this, 1);//结业证书查询


        Bitmap qrCode = QRCodeUtil.createQRCode( "http://www.cqjtgx.com:8083/jt/site/report/searchself.jsp?type=1001&jyid="+uschSn);
        img_code.setImageBitmap(qrCode);
    }
    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        if(what==1)
        {
            CertificateCompletionBean certificateCompletionBean = JSON.parseObject(result, CertificateCompletionBean.class);
            if(certificateCompletionBean!=null)
            {
                CertificateCompletionBean.DataBean dataBean = certificateCompletionBean.getData();

                String userName = dataBean.getUserName();//姓名
                String userMobile = dataBean.getUserMobile();//手机号
                String idcNo = dataBean.getIdcNo();//身份证
                long beginDate = dataBean.getBeginDate();//开始日期
                long endDate = dataBean.getEndDate();//结束日期
                String certNo = dataBean.getCertNo();//编号

//                if(userName!=null)
//                {
//                    tv_education3.setText(userName);
//                }
//                if(idcNo!=null)
//                {
//                    tv_education6.setText(idcNo);
//                }


                Date begindate1=new Date(beginDate);
                Date enddate1=new Date(endDate);
                try {
                    //yyyy表示年MM表示月dd表示日
                    //yyyy-MM-dd是日期的格式，比如2015-12-12如果你要得到2015年12月12日就换成yyyy年MM月dd日
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
                    SimpleDateFormat sdf1=new SimpleDateFormat("yyyyMMdd");
                    //进行格式化
                    begindateStr =sdf.format(begindate1);
                    enddateStr =sdf.format(enddate1);
                    enddateStr1 =sdf1.format(enddate1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                String str="\u3000\u3000\u3000\u3000姓名：   " + userName + "        身份证号：   " + idcNo + "       参加重庆市交通干部";
                SpannableString spanString = new SpannableString(str);
                UnderlineSpan span = new UnderlineSpan();
                spanString.setSpan(span, 7, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //尝试用固定下标
                SpannableString spanString1 = new SpannableString(spanString);
                UnderlineSpan span1 = new UnderlineSpan();
                spanString1.setSpan(span1, 26, 49, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //尝试用固定下标
                tv_education2.setText(spanString1);
                tv_education3.setText("学校 （交通行业职业培训中心） 组织的     " + begindateStr + "到" + enddateStr + "      道路运");
                tv_education4.setText("输驾驶员网络远程继续教育培训，考试成绩合格。");
                tv_education11.setText(enddateStr);
                if(certNo==null) {
                    tv_education1.setText("编号：" + enddateStr1 + userMobile);
                }else{
                    tv_education1.setText("编号：" + certNo);
                }

            }

        }
        super.onSuccess(result, call, response, what);
    }

    @Override
    public void onError(String result, Call call, Response response, int what) {
        if (what==1) {
            CheckBean checkBean = JSON.parseObject(result, CheckBean.class);
            String msg = checkBean.getMsg();
            TiShiDialog.Builder builder1 = new TiShiDialog.Builder(ContinuingEducationActivity.this);
            builder1.setMessage(msg);
            builder1.setTitle("提示:");
            builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    ;
                    //设置你的操作事项
                }
            });
            builder1.create().show();

        }
        super.onError(result, call, response, what);
    }
}
